<?

	include("inc/config.inc.php");
    
        include("inc/header.inc.php");

	// Get year if passed, otherwise use current
	if (is_numeric($_GET[y]))
		$year = mysqli_real_escape_string($con,$_GET[y]);
	else
		$year = date("Y");

	// Get month if passed, otherwise use current - remove any leading zeros - for the calendar function
	if (is_numeric($_GET[m]))
	{
		$month = mysqli_real_escape_string($con,$_GET[m]);
		if (substr($month, 0, 1) == 0)
			$month = substr($month, 1, 1);
	}
	else
		$month = date("n");
     
	echo "<h2><span class='textColorChange'>$config[systemtitle]</span></h2><br/>\n\n";    
      
	echo "<a href='main.php' style='color:black;font-size:10pt'><< Back</a><br/>";
                

	echo "<div style='background:#504E52' class='normalText'>\n";
	echo "<b>Requests Needing Approval</b><br/>\n";

	// Join request and user tables to get requests for this manager viewing
	$result = mysqli_query($conn, "SELECT requests.requestid, requests.user, users.manager, requests.description, requests.startdate, requests.enddate, requests.starttime, requests.endtime, requests.days FROM users, requests WHERE (users.user = requests.user) AND users.manager='$_SESSION[user]' AND requests.status='pending' ORDER BY startdate ASC");
	if (mysqli_num_rows($result) > 0)
	{
		echo "<br/><table border='1'><tr><td><b>User</b></td><td><b>Description&nbsp;</b></td><td><b>Start Date</b>&nbsp;</td><td><b>End Date</b>&nbsp;</td><td><b>Days</b>&nbsp;</td><td><b>Action</b></td></td>";
		while ($request = mysqli_fetch_array($result))
		{

			// Convert DB am/pm into times to make more logical for humans
			if ($request[starttime] == am)
				$request[starttime] = "09:00";
			if ($request[starttime] == pm)
				$request[starttime] = "12:00";
			if ($request[endtime] == am)
				$request[endtime] = "12:00";
			if ($request[endtime] == pm)
				$request[endtime] = "17:00";

			echo "<tr><td>$request[user]&nbsp;</td>\n";
			echo "<td>$request[description]&nbsp;</td>\n";
			echo "<td>" . date("d/m/Y", strtotime($request[startdate])) . " - $request[starttime]&nbsp;</td>\n";
			echo "<td>" . date("d/m/Y", strtotime($request[enddate])) . " - $request[endtime]&nbsp;</td>\n";
			echo "<td align='center'>" . str_replace(".0","",$request[days]) . "</td> <td><small>\n";
			
			// Find any other requests that clash so we can list them
			$cResult = mysqli_query($conn, "SELECT * FROM requests WHERE '$request[startdate]' <= enddate AND startdate <= '$request[enddate]' AND (status='Pending' OR status='Accepted') AND requestid!='$request[requestid]'");
			if (mysqli_num_rows($cResult) > 0)
			{

				// jQuery for hoverboxes that show the clash details
				echo "
				<script>
				$(function() {
					$('a#clash-$request[requestid]').hover(function(e) {
						$('div#clashpop-$request[requestid]').show()
						.css('top', e.pageY + 15)
						.css('left', e.pageX + 15)
						.appendTo('body');
						}, function() {
						$('div#clashpop-$request[requestid]').hide();
					});
					$('a#clash-$request[requestid]').mousemove(function(e) {
						$('div#clashpop-$request[requestid]').css('top', e.pageY + 15).css('left', e.pageX + 15);
					});
				});
				</script>
				";

				// Content for the clashbox
				echo "<a href='approvals.php?y=" . substr($request[startdate], 0, 4) . "&m=" . substr($request[startdate], 5, 2) . "' id='clash-$request[requestid]'><img src='img/clash.png' width='16' height='16' border='0'>View Clashes</a> <br\n>";
				echo "<div id='clashpop-$request[requestid]' class='clashPop'>\n";
				echo "<table border='1'><tr><td>User</td><td>Status</td><td>Start</td><td>End</td></tr>\n";
				while ($clash = mysqli_fetch_array($cResult))
				{
				
					// Colour clash username to show pending or accepted
					if ($clash[status] == "Pending")
						$sCol = "Blue";
					else
						$sCol = "Green";
						
					echo "<tr><td>$clash[user]</td>\n";
					echo "<td style='color:$sCol'>$clash[status]</td>\n";
					echo "<td>" . date("d/m/Y", strtotime($clash[startdate])) . "</td>\n";
					echo "<td>" . date("d/m/Y", strtotime($clash[enddate])) . "</td></tr>\n";
				}
				echo "</table>\n";
				echo "</div>\n";
			}
			else
				echo "<a href='#' id='clash-$request[requestid]'><img src='img/noclash.png' width='16' height='16' border='0'>No Clashes</a> <br\n>";


			echo "<a href='approve.php?r=$request[requestid]&a=Accepted'><img src='img/accept.png' width='16' height='16' border='0'>Accept</a> <br/>\n";
			echo "<a href='approve.php?r=$request[requestid]&a=Rejected'><img src='img/reject.png' width='16' height='16' border='0'>Reject</a></small></td></tr>\n";
		}
		
		echo "</table>";
	}

	echo "</div>\n";
	echo "\n<br/>\n\n";

                                
	echo "<div style='background:#504E52' class='normalText'>\n";
	echo "<b>All Holidays</b><br/>\n";
	
	$nextYear = $year;
	$prevYear = $year;
	$nextMonth = $month+1;
	$prevMonth = $month-1;
	
	if ($prevMonth == 0)
	{
		$prevMonth = 12;
		$prevYear--;
	}
	
	if ($nextMonth == 13)
	{
		$nextMonth = 1;
		$nextYear++;
	}
	
	echo "<a href='approvals.php?m=$prevMonth&y=$prevYear'><b>&lt;&lt</b></a> ";
	echo date('F', mktime(0, 0, 0, $month)) . " $year ";
	echo "<a href='approvals.php?m=$nextMonth&y=$nextYear'><b>&gt;&gt</b></a>";
	echo draw_calendar($month,$year);

	echo "</div>\n";
	echo "\n<br/>\n\n";



	include("inc/footer.inc.php");
    
?>
    
